var util = require('../../utils/util.js')
var app = getApp();
var md5 = require('../md5.js');
Page({
  data: {
  
    
  },
  submit: function () {
    this.setData({
      showModal: true
    })
  },
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh({
      complete: (res) => { },
    });
  },

  preventTouchMove: function () {

  },


  go: function () {
    this.setData({
      showModal: false
    })
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    var that = this
    // 页面初始化 options为页面跳转所带来的参数
    var newsid = options.newsid
    console.log('newsid='+newsid)
    this.setData({
      newsid: newsid
    })
    //设置语言0:中文，1：英文
    var setlanguage = wx.getStorageSync('setlanguage')
    if ((wx.getStorageSync('setlanguage') || !"".trim() == wx.getStorageSync('setlanguage'))) {
      this.setData({
        setlanguage: wx.getStorageSync('setlanguage')
      })
      if (setlanguage == 1) {
        wx.setNavigationBarTitle({
          title: 'News detail'
        })
      } else {
        wx.setNavigationBarTitle({
          title: '新闻详情'
        })
      }
    } else {
      wx.setStorageSync('setlanguage', 0);
      this.setData({
        setlanguage: 0
      })
    }
    //获取缓存展会id和用户id存在则获取最新联系人列表
    var exhibitionid = wx.getStorageSync('exhibitionid')
   // exhibitionid = '33'
    var u = wx.getStorageSync('u')
    console.log('exhibitionid=', exhibitionid)
    console.log('u=', u)
    if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
      && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var cururl = app.globalData.picoaddress + '/attendee/news/detail'
      var m = {}
      m["v"] = '1.0.0'
      m["client"] = 'ios'
      m["timestamp"] = timestamp
      m["lan"] = 'cn'
      m["exhibitionid"] = exhibitionid
      m["newsid"] = newsid
      // m["attendeeid"] = attendeeid
      //联系人id
      m["u"] = u
      var sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'newsid' + newsid + 'timestamp' + timestamp + app.globalData.picotoken
      console.log(sign)
      var curb64 = md5.zhMD5(sign)
      console.log(curb64)
      m["sign"] = curb64
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: cururl,
        method: 'post',
        data: m,
        success(res) {
          console.log(res)
          var mesg = res.data
          console.log('mesg=' + mesg)
          var curdata = mesg.data
          var content = curdata.content+''
          console.log('content=' + content)
          content = content.replace(/\<img/g,'<img class="img" style="width:100px!import;height:200px!import;display:"block"')
          console.log('thcontent=' + content)
          curdata.content = content
          that.setData({
            'newsDetailsMesg': curdata
          });

        }, fail: res => {
          console.log(res, "error")
        }
      })

    } else {
      wx.redirectTo({
        url: '../signInbyPhone/signIn'
      })
    }
  },
  sc: function () {
    var that = this
    var newsid = that.data.newsid
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var u = wx.getStorageSync('u')
    console.log('exhibitionid=' + exhibitionid)
    console.log('u=' + u)
    if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
      && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
      //默认最新（调用pico接口）
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var url = app.globalData.picoaddress + '/attendee/news/collect'
      var k = {}
      k["v"] = '1.0.0'
      k["client"] = 'ios'
      k["timestamp"] = timestamp
      k["lan"] = 'cn'
      k["exhibitionid"] = exhibitionid
      k["newsid"] = newsid
      k["u"] = u
      var sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'newsid' + newsid + 'timestamp' + timestamp + app.globalData.picotoken
      console.log(sign)
      var curb64 = md5.hexMD5(sign)
      console.log(curb64)
      k["sign"] = curb64
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: url,
        method: 'post',
        data: k,
        success(res) {
          console.log(res)
          var exhibitionid = wx.getStorageSync('exhibitionid')
          // exhibitionid = '33'
          var u = wx.getStorageSync('u')
          console.log('exhibitionid=', exhibitionid)
          console.log('u=', u)
          if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
            && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
            var timestamp = Date.parse(new Date());
            timestamp = timestamp / 1000;
            var cururl = app.globalData.picoaddress + '/attendee/news/detail'
            var m = {}
            m["v"] = '1.0.0'
            m["client"] = 'ios'
            m["timestamp"] = timestamp
            m["lan"] = 'cn'
            m["exhibitionid"] = exhibitionid
            m["newsid"] = newsid
            // m["attendeeid"] = attendeeid
            //联系人id
            m["u"] = u
            var sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'newsid' + newsid + 'timestamp' + timestamp + app.globalData.picotoken
            console.log(sign)
            var curb64 = md5.zhMD5(sign)
            console.log(curb64)
            m["sign"] = curb64
            wx.request({
              header: {
                'content-type': 'application/x-www-form-urlencoded',
              },
              url: cururl,
              method: 'post',
              data: m,
              success(res) {
                wx.showToast({
                  title: '操作成功！',
                  icon: 'success',
                  duration: 2000
                })
                console.log(res)
                var mesg = res.data
                console.log('mesg=' + mesg)
                var curdata = mesg.data
                var content = curdata.content + ''
                console.log('content=' + content)
                content = content.replace(/\<img/g, '<img class="img" style="width:100px!import;height:200px!import;display:"block"')
                console.log('thcontent=' + content)
                curdata.content = content
                that.setData({
                  'newsDetailsMesg': curdata
                });


              }, fail: res => {
                console.log(res, "error")
              }
            })

          } 
        
        }, fail: res => {
          console.log(res, "error")
        }

      })

    } else {
      wx.redirectTo({
        url: '../signInbyPhone/signIn'
      })
    }
  },
  onShareAppMessage: function (res) {
    var that = this
    let users = wx.getStorageSync('user');
    var newsid = that.data.newsid
    if (res.from === 'button') { }
    return {
      title: '新闻咨询',
      path: 'pages/news/newsDetails?newsid=' + newsid,
      success: function (res) { }
    }
  },
})